﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Common;

#endregion

namespace ScrumTable.BL.DM.DataManagement.Tracking.EditCommands
{
    /// <summary>
    /// Contains all required information to perform undo / redo actions.
    /// </summary>
    internal class DomainObjectUndoEditCommand : IUndoEditCommand
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly ValueChain<Pair<string, Type>> _typedIdChain;
        private readonly IDataObjectState _stateBeforeChange;
        private readonly IDataObjectState _stateAfterChange;
        private readonly string _propertyName;
        private readonly string _propertyDisplayName;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the state change cause.
        /// </summary>
        public string Cause { get { return _propertyDisplayName; } }

        /// <summary>
        /// Gets the change type which means the element or action that
        /// caused the undo edit entry.
        /// </summary>
        /// <value></value>
        public UndoEditType Type { get { return UndoEditType.ValueChange; } }

        /// <summary>
        /// Gets/sets the associated domain context.
        /// </summary>
        public IDomainContext Context { get; set; }

        /// <summary>
        /// Gets the name of the domain object which accommodates the change. 
        /// </summary>
        public string ElementName { get; private set; }


        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DomainObjectUndoEdit instance.
        /// </summary>
        internal DomainObjectUndoEditCommand(
            string name,
            ValueChain<Pair<string, Type>> typedIdChain,
            IDataObjectState stateBeforeChange,
            IDataObjectState stateAfterChange,
            string changedPropertyName,
            string propertyDisplayName)
        {
            PreCondition.AssertNotNull(typedIdChain, "typedIdChain");
            PreCondition.AssertNotNull(stateBeforeChange, "stateBeforeChange");
            PreCondition.AssertNotNull(stateAfterChange, "stateAfterChange");

            _typedIdChain = typedIdChain;
            _stateBeforeChange = stateBeforeChange;
            _stateAfterChange = stateAfterChange;

            _propertyName = changedPropertyName;
            _propertyDisplayName = (propertyDisplayName ?? changedPropertyName);
            ElementName = name ?? string.Empty;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Executes the underlying undo command.
        /// </summary>
        void IUndoEditCommand.CallUndo()
        {
            PerformCommand(_stateBeforeChange);
        }

        /// <summary>
        /// Executes the underlying redo command.
        /// </summary>
        void IUndoEditCommand.CallRedo()
        {
            PerformCommand(_stateAfterChange);
        }

        private void PerformCommand(IDataObjectState state)
        {
            DomainBase domainObject = Context.TryGetObjectByChain(_typedIdChain);

            if (domainObject != null)
            {
                domainObject.DataObject.ImportState(
                    state,
                    schemaBase => domainObject.FirePropertyChanging(_propertyName),
                    schemaBase => domainObject.FirePropertyChanged(_propertyName));

                domainObject.Save();
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}